package com.teamone.etl.exception;

import com.baomidou.mybatisplus.extension.api.R;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description 统一异常处理
 * @Author WangZhuoRan
 * @Date 2021-01-14 20:55
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    @Resource
    private HttpServletRequest request;

    @Resource
    private HttpServletResponse response;

    @ExceptionHandler(CustomException.class)
    public R<String> doError(CustomException cx){
        return R.failed(cx.getCommonCode().msg());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public R<String> handleValidException(MethodArgumentNotValidException e){
        StringBuilder errorMessage = new StringBuilder();
        e.getBindingResult().getFieldErrors().forEach(fieldError -> errorMessage.append(fieldError.getDefaultMessage()).append(","));
        return R.failed(errorMessage.deleteCharAt(errorMessage.length()-1).toString());
    }
}

